package com.dx.login;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

import com.dx.login.User;
import com.jfinal.core.Controller;
import com.jfinal.ext.render.CaptchaRender;
import com.jfinal.validate.Validator;

public class RegistValidator extends Validator {
	protected void validate(Controller controller) {
		String authCode = controller.getPara(controller.getPara("registType")
				+ "Authcode");
		if (!StringUtils.isBlank(authCode)) {
			System.out.println("systemRandomCode:"
					+ controller.getSessionAttr("systemRandomCode"));
			if (!CaptchaRender.validate(String.valueOf(controller
					.getSessionAttr("systemRandomCode")), authCode
					.toUpperCase())) {
				addError(controller.getPara("registType") + "AuthcodeMsg",
						"验证码输入不正确,请重新输入");
			}
		} else {
			addError(controller.getPara("registType") + "AuthcodeMsg", "请输入验证码");
		}

		String account = "";
		if ("email".equals(controller.getPara("registType"))) {
			System.out.println("邮箱注册用户");
			account = controller.getPara("emailEmail");
			validateEmail("emailEmail", "emailEmailMsg", "请输入正确邮箱!");
			validateRequiredString("emailPassword", "emailPasswordMsg",
					"请输入密码!");
			validateRequiredString("emailConfirmPassword",
					"emailConfirmPasswordMsg", "请确认密码!");
			validateRequiredString("emailAuthcode", "emailAuthcodeMsg",
					"请输入验证码!");

			if (!StringUtils.isBlank(controller.getPara("emailPassword"))
					&& !controller.getPara("emailPassword").equals(
							controller.getPara("emailConfirmPassword"))) {
				addError("emailConfirmPasswordMsg", "两次输入密码不一致");
			}

		} else {
			System.out.println("手机注册用户");
			account = controller.getPara("telTel");
			validateRequiredString("telTel", "telTelMsg", "请输入手机号!");
			// validateEmail("telEmail", "telEmailMsg", "请输入正确邮箱!");

			validateRequiredString("telPassword", "telPasswordMsg", "请输入密码!");
			validateRequiredString("telConfirmPassword",
					"telConfirmPasswordMsg", "请确认密码!");
			validateRequiredString("telAuthcode", "telAuthcodeMsg", "请输入验证码!");
			if (!StringUtils.isBlank(controller.getPara("telPassword"))
					&& !controller.getPara("telPassword").equals(
							controller.getPara("telConfirmPassword"))) {
				addError("telConfirmPasswordMsg", "两次输入密码不一致");
			}

			if (!StringUtils.isBlank(controller.getPara("telEmail"))) {
				validateEmail("telEmail", "telEmailMsg", "请输入正确邮箱!");
			}
			if (!StringUtils.isBlank(controller.getPara("telTel"))&&!isMobile(controller.getPara("telTel"))) {
				addError("telTelMsg", "请输入正确手机号");
			}
		}
		// 如果全部验证通过，则验证账号是否重复
		if (!this.getInvalid()) {
			if (User.dao.findById(account) != null) {
				if ("email".equals(controller.getPara("registType"))) {
					addError("emailEmailMsg", "该邮箱已被注册!");
				} else {
					addError("telTelMsg", "该手机号已被注册!");
				}
			}
		}

	}

	protected void handleError(Controller controller) {
		controller.keepModel(User.class);
		controller.keepPara();

		// System.out.println("getParaMap:"+controller.getParaMap());
		// System.out.println("email.email:"+controller.getAttr("email.email"));;
		String actionKey = getActionKey();
		controller.render("/WEB-INF/jsp/login/regist.jsp");

		// if (actionKey.equals("/blog/save"))
		// controller.render("add.jsp");
		// else if (actionKey.equals("/blog/update"))
		// controller.render("edit.jsp");
	}

	/**
	 * 验证手机号码
	 * @param mobiles
	 * @return true/false
	 */

	public static boolean isMobile(String mobiles) {
		boolean flag = false;
		try {
			Pattern p = Pattern
					.compile("^((13[0-9])|(15[0-9])|(18[0,4-9]))\\d{8}$");
			Matcher m = p.matcher(mobiles);
			flag = m.matches();
		} catch (Exception e) {
			flag = false;
		}
		return flag;
	}
}
